﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DAL;
using DevExpress.Web.ASPxTreeList;
using System.Data.SqlClient;
using System.Data;
namespace EDMS.UserRight
{
    public partial class RightShow : Frame.Pages.BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            //显示数据treelist
            base.Page_Load(sender, e);
            tvShow();
        }
        protected override string ActionID
        {
            get { return "0"; }
        }
        void tvShow()
        {
            DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable("select * from Functions");
            this.DxTreelist.DataSource = dt;
            this.DxTreelist.DataBind();

        }
        //实现选择不同的人员显示人员的权限信息
        protected void DxTreelist_CustomCallback(object sender, DevExpress.Web.ASPxTreeList.TreeListCustomCallbackEventArgs e)
        {
            string[] Rights;
            //选择不同人员后刷新当前人员的权组信息
            int id = Convert.ToInt32(e.Argument);
            string strSql = "select Rights from [User] where ID = " + id;
            string right = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar(strSql).ToString();
            Rights = right.Split('|');
            //清除所有选择
            for (int i = 0; i < this.DxTreelist.Nodes.Count; i++)
            {
                this.DxTreelist.Nodes[i].Selected = false;
            }
            //确定是否有权限 显示权限
            if (Rights.Length > 0)
            {
                for (int i = 0; i < Rights.Length; i++)
                {
                    if (!Rights[i].Equals(""))
                    {
                        TreeListNode selection = DxTreelist.FindNodeByKeyValue(Rights[i]);    //查找有权限的项
                        //如果有权限则设置为true
                        if (selection != null)
                        {
                            selection.Selected = true;
                        }
                    }
                }
            }
        }

        //实现修改并保存当前人员的权限信息
        protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            //得到从客户端传递过来的参数来进行异步通信响应
            if (e.Parameter.Equals("null"))
            {
                throw new Exception("请选择人员！");
            }
            string right = "";
            if (this.DxTreelist.GetSelectedNodes().Count > 0)   //判断treelist是否勾选了权限信息
            {
                List<TreeListNode> selection = this.DxTreelist.GetSelectedNodes();   //获取选定的权限信息
                //遍历node并连接成字符串
                foreach (TreeListNode node in selection)
                {
                    right = right + node.GetValue("FunctionID") + "|";
                }
            }

            //实现修改指定人员的权限信息并保存
            List<SqlParameter> Params = new List<SqlParameter>();
            Params.Add(new SqlParameter("@ID", e.Parameter));
            Params.Add(new SqlParameter("@right", right));
            Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("update [User] set Rights=@right where ID=@ID", Params.ToArray());
        }
    }
}